package com.settlement.system.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.settlement.system.common.base.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
* <p>
* 京东订单
* </p>
*
* @author huangrensen
* @since 2024-04-13
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JdOrder extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 平台ID
     */
    private String pId;


    /**
    * 网店交易号
    */
    private String orderNo;


    /**
    * 订单状态
    */
    private String orderStatus;


    /**
    * 支付状态
    */
    private String payStatus;


    /**
    * 下单时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderTime;


    /**
    * 付款时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime payTime;


    /**
    * 发退货时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime sendTime;


    /**
    * 出库时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime outboundTime;


    /**
    * 数量
    */
    private Integer quantity;


    /**
    * 实付金额
    */
    private BigDecimal payAmount;


    /**
    * 退货数量
    */
    private Integer returnQty;


    /**
    * 退货金额
    */
    private BigDecimal returnAmount;


    private Long createBy;


    private Long updateBy;


    /**
    * 所属平台
    */
    private String platform;


    /**
    * 店铺ID
    */
    private Long storeId;
    private String storeName;

    @TableField(exist = false)
    private String tableName;

    public static boolean isDate(String field) {
        return "orderTime,payTime,sendTime,outboundTime".contains(field);
    }

    public static boolean isDecimal(String field) {
        return ",payAmount,returnAmount,".contains(","+field+",");
    }

    public static boolean isInteger(String field) {
        return "quantity,returnQty".contains(field);
    }

    private static boolean compareDate(LocalDateTime date1, LocalDateTime date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 != null && date1.equals(date2)) {
            return true;
        }
        return false;
    }

    private static boolean compareBigDecimal(BigDecimal value1, BigDecimal value2) {
        if (value1 == null && value2 == null) {
            return true;
        }
        if (Objects.isNull(value2)) {
            value2 = BigDecimal.ZERO;
        }
        return value1 != null && value1.compareTo(value2) == 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        JdOrder jdOrder = (JdOrder) o;
        return Objects.equals(orderNo, jdOrder.orderNo) && Objects.equals(orderStatus, jdOrder.orderStatus) && Objects.equals(payStatus, jdOrder.payStatus) && compareDate(orderTime, jdOrder.orderTime) && compareDate(payTime, jdOrder.payTime) && compareDate(sendTime, jdOrder.sendTime) && compareDate(outboundTime, jdOrder.outboundTime) && Objects.equals(quantity, jdOrder.quantity) && compareBigDecimal(payAmount, jdOrder.payAmount) && Objects.equals(returnQty, jdOrder.returnQty) && compareBigDecimal(returnAmount, jdOrder.returnAmount) && Objects.equals(createBy, jdOrder.createBy) && Objects.equals(updateBy, jdOrder.updateBy) && Objects.equals(platform, jdOrder.platform) && Objects.equals(storeId, jdOrder.storeId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), orderNo, orderStatus, payStatus, orderTime, payTime, sendTime, outboundTime, quantity, payAmount, returnQty, returnAmount, createBy, updateBy, platform, storeId);
    }
}
